package srzy.driver

import srzy.data.PublicData
import srzy.common.Logging
import srzy.socket.DCCMsgActor
import srzy.module.CaseDeal
import srzy.common.Common
import srzy.diameter._
import srzy.socket._
import srzy.dao.DBUtil
import java.io.File
import srzy.common.Config
import java.net.ServerSocket
import srzy.common.TnsParser
import srzy.common.AltibaseConnStrParser

object Start1 extends Logging {

  def main(args: Array[String]): Unit = {
    //    println(getRequestFlag(64L))
    //    println(getRequestFlag(128L))
    deal7
  }

  def deal11 = {
    PublicData
    try {
      logger.debug("start DCCMsgActor")
      DCCMsgActor().start
      logger.debug("start DCCMsgActor over")
      logger.debug("start RBMsgTransitActor")
      RBMsgTransitActor
      logger.debug("start RBMsgTransitActor over")

    } catch {
      case ex: Exception => {
        logger.error(ex.getMessage(), ex)
        sys.exit
      }
    }
  }

  def deal10 = {
    val str = srzy.common.readFromFile("1.xml").mkString("").replaceAll(" ", "")
    //    val str = "OCS=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.168.1.245)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=OCS)))"
    //    val str = "DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=172.168.1.245)(PORT=1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=OCS))"
    logger.debug(str)
    val l = TnsParser.parse(str)
    logger.debug(l)

  }

  //  def deal2 = {
  //    val l = DBUtil("CaseInfoSource")
  //    val ll = l.getCaseInfo.get("data_1.2.02.xml").get
  //    val lll = ll._2
  //    println(lll)
  //    println("---")
  //    println(lll.split("\n").toList.mkString("\n"))
  //    //    l.updateCaseInfo("1.xml", "aa", "bb", "cc", "dd")
  //  }

  def deal3 = {
    RBMsgTransitActor

  }

  def deal4 = {
    val l = (<command name="CCR">
               <avp value="data;1.1.07" name="Session-Id"></avp>
               <avp value="sm-all-test" name="Origin-Host"></avp>
               <avp value="chinaunicom.com" name="Origin-Realm"></avp>
               <avp value="3618177240" name="Event-Timestamp"></avp>
               <avp name="Service-Information">
                 <avp name="PS-Information">
                   <avp value="0" name="3GPP-PDP-Type"></avp>
                   <avp value="0x0001A15CC3" name="PDP-Address"></avp>
                   <avp value="5" name="3GPP-NSAPI"></avp>
                   <avp value="0x0001" name="3GPP-RAT-Type"></avp>
                   <avp value="0400" name="3GPP-Charging-Characteristics"></avp>
                   <avp value="-1467184619" name="3GPP-Charging-Id"></avp>
                 </avp>
               </avp>
             </command>)
    val l2 = l.child.toList.filter(x => x.attribute("name") match {
      case Some(v) => if (v.text == "Session-Id") true else false
      case None => false
    }).map(x => (x \\ "@value").head.text).head
    println(l2)
  }

  //  def deal5 = {
  //    val l = DBUtil.apply("CaseInfoSource")
  //    l.updateCaseInfo("in_1.5.06.xml", "wlan", "aaa", "bbb", "ccc", "ddd")
  //    //    println(l.getCaseInfo)
  //  }

  def deal7 = {
    import srzy.rb.RBMsg
    val stdMsg = "[100=60][101=20140926165341][000=sm.auto.test;voice_1.2.27][001=2078081271465938][R65=0]"
    val realMsg = "[100=60][101=20140926165341][000=sm.auto.test;voice_1.2.27][001=6258424480270290][R65=0][EVT=1]"

    logger.debug(RBMsg.compare2(stdMsg, realMsg))

  }

  def deal8 = {
    import srzy.rb.RBMsg
    val stdMsg = "[100=90][101=20140828142006][000=hqq_cxvas_20141021423][001=3464148826556729308][R85=1][R01=13200007777][R02=13200007777][R03=13253370160][R40=000,R401=481,R402=10,R405=2,R406=8888,R407=100,R408=ClientIP,R409=chb_cxvas_2014092316290,R410=0][R50=000,R503=6000025501,R504=0556,R506=3,R507=010,R509=3,R5012=0556][R60=000,R601=0,R602=4,R603=0,R604=20141229130215,R605=0][B03=000,B036=6,B037=1,B038=0,B0391=2;B03=000,B036=2,B037=100,B038=0,B0391=2][B06=1][B07=1][B08=1][B20=1][B21=1][B15=1]"
    val realMsg = "[100=90][101=20140828142006][000=hqq_cxvas_20141021423][001=3462830512115025884][R85=1][R01=13200007777][R02=13200007777][R03=13253370160][R40=000,R401=481,R402=10,R405=2,R406=8888,R407=100,R408=ClientIP,R409=chb_cxvas_2014092316290,R410=0][R50=000,R503=6000025501,R504=0556,R506=3,R507=010,R509=3,R5012=0556][R60=000,R601=0,R602=4,R603=0,R604=20141229130934,R605=0][B03=000,B036=6,B037=1,B038=0,B0391=2;B03=000,B036=2,B037=100,B038=0,B0391=2][B06=1][B07=1][B08=1][B20=1][B21=1][B15=1]"

    println(stdMsg.substring(stdMsg.indexOf("[") + 1,
      stdMsg.lastIndexOf("]")).split("\\]\\[").toList.mkString("\n"))
  }

  def deal9 = {

    def leftPad(str: String, i: Int, pad: Char = '0') = (List.fill(i)(pad).mkString("") + str).substring(str.length())

    //    val l = new java.io.File("case/wlan")
    //    l.listFiles().toList.foreach(x => {
    //      val n = x.getName()
    //      val regex = """([0-9]+).([0-9]+).([0-9]+)_([a-z]+).xml""".r
    //      val regex(d1, d2, d3, t) = n
    //      val newName = t + "_" + d1 + "." + d2 + "." + leftPad(d3, 2) + ".xml"
    //      logger.debug(newName)
    //      x.renameTo(new File("case/wlan/" + newName))
    //    })
    val l = new java.io.File("case").listFiles().toList.filter(_.isDirectory()).flatMap(_.listFiles().toList)
    l.foreach(x => {
      val name = x.getName()
      val regex = """([a-z]+)_([0-9.]+).xml""".r
      val regex(t, id) = name
      //      val caseName = t + ";" + id
      //      val fl = Common.readFromFile(x)
      //      val fl2 = fl.map(y => {
      //        val r = """<avp[ ]+name="Session-Id"""".r
      //        r.findFirstIn(y) match {
      //          case Some(v) => """      <avp name="Session-Id" value="""" + caseName + """"></avp>"""
      //          case None => y
      //        }
      //      })
      //      Common.writeToFile(x, fl2.mkString("\n"))

      //      logger.debug("caseName:" + caseName)

      println("1\t" + name + "\t" + t)
    })
  }
}

